package com.ms.game.common;

import java.math.BigDecimal;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.common.container.GameOptionData;
import com.ms.game.common.container.GameOptionSetting;
import com.xpec.c4.game.common.Helper;

public class GameOptionInfo {
	
	private static final Log log = LogFactory.getLog(GameOptionInfo.class);
		
	public static final String HJP1 = "HJP1";
	
	public static final String HJP2 = "HJP2";
	
	public static final String HJP3 = "HJP3";
	
	/**
	 * 讀取遊戲吐水吐過頭容許值
	 * 
	 * @param game
	 *            {@link GameType}
	 * @param halltype
	 * @return
	 */
	public static int getPourOverMoney(String game , String halltype)
	{
		GameOptionData op = getData(game,halltype);
		if(op==null)
		{
			return 0;
		}
		
		return op.getPourOverMoney();
	}
	
	public static int getRookiePourOverMoney(String game , String halltype)
	{
		GameOptionData op = getData(game,halltype);
		if(op==null)
		{
			return 0;
		}
		
		return op.getRookiepourOverMoney();
	}
	
	
	/**
	 * 讀取遊戲水庫封存值
	 * 
	 * @param game
	 *            {@link GameType}
	 * @param halltype
	 * @return
	 */
	public static int getResetMoney(String game , String halltype)
	{
		GameOptionData op = getData(game,halltype);
		if(op==null)
		{
			// 沒資料的話給他一個極大值, 避免程式運作出問題, 防呆容錯用
			return 100000000;
		}
		
		return op.getResetMoney();
	}
	
	/**
	 * 取得每個遊戲押注轉到hjp的比例
	 * @param gameType 遊戲
	 * @param hallType 館別
	 * @param hjpKey  {@link #HJP1}  {@link #HJP2}  {@link #HJP3}
	 *
	 * @return
	 */
	public static Double getGameBetToHjpRate(String gameType, String hallType, String hjpKey) {
		HashMap<String , HashMap<String,GameOptionData>> gameOptionDatas = GameOptionSetting.getInstance().getDataMap();
		
		if(!gameOptionDatas.containsKey(gameType)){
			printErrorLogAndRunTimeException("[getGameBetToHjpRate] gameType error, gameType =" + gameType);
			return null;
		}
		
		HashMap<String, GameOptionData> hallData = gameOptionDatas.get(gameType);
		if(!hallData.containsKey(hallType)){
			printErrorLogAndRunTimeException("[getGameBetToHjpRate] hallType error, hallType =" + hallType);
			return null;
		}
		
		GameOptionData gameOptionData = hallData.get(hallType);
		
		HashMap<String, Double> betToHjpRate = gameOptionData.getBetToHjpRate();
		
		if(!betToHjpRate.containsKey(hjpKey)){
			printErrorLogAndRunTimeException("[getGameBetToHjpRate] hjpKey error, hjpKey =" + hjpKey);
			return null;
		}
		
		return betToHjpRate.get(hjpKey);
		
	}
	
	/**
	 * 取得押注額真正要進到hjp的錢
	 * @param gameType
	 * @param hallType
	 * @param hjpKey
	 * @param betDraw 押注額 x GameBaseSetting上的hjp rate
	 * @return
	 */
	public static BigDecimal getRealHjpDraw(String gameType, String hallType, String hjpKey, BigDecimal betDraw) {
		
		Double hjpDrawRate = getGameBetToHjpRate(gameType, hallType, hjpKey);
		
		BigDecimal hjpDraw = Helper.mul(betDraw, new BigDecimal(hjpDrawRate.toString()));

		return hjpDraw;
	}
	
	private static void printErrorLogAndRunTimeException(String error){
		if(log.isErrorEnabled()){
			log.error(error, new RuntimeException());
		}
	}
	
	/**
	 * 讀取遊戲各館資料
	 * @param game
	 * @param halltype
	 * @return
	 */
	public static GameOptionData getData(String game , String halltype)
	{
		if(!GameOptionSetting.getInstance().getDataMap().containsKey(game))
		{
			return null;
		}
		
		HashMap<String,GameOptionData>  data = GameOptionSetting.getInstance().getDataMap().get(game);
		if(data.containsKey(halltype))
		{
			return data.get(halltype);
		}
		
		return null;
	}
}
